﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Enferno.Public.InversionOfControl;
using Enferno.Public.Web.Builders;
using Enferno.Public.Web.Mappers.ProductProfiles;
using Enferno.Public.Web.Mappers.Resolvers;
using Enferno.Public.Web.Models;
using Enferno.StormApiClient.Products;
using Enferno.Web.StormUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace Enferno.Public.Web.Test.Extending
{
    [TestClass]
    public class ExtendedProductListTests
    {
        [TestInitialize]
        public void Initialize()
        {
            var rules = MockRepository.GenerateMock<ISiteRules>();
            IoC.RegisterInstance(typeof (ISiteRules), rules);
            Mapper.AddProfile<ProductProfile>();
        }

        //**************************************************** Test 1 ****************************************************

        //the new product model, here we want to include the EanCode property that is also present in the product entity
        private class ExtendedProductItemModel : ProductItemModel
        {
            public string CampaignImage { get; set; }
        }

        //the product builder that can handle the new extended product model
        private class ExtendedProductListBuilder :
            ProductListCustomProductItemBuilder<ExtendedProductItemModel>
        {
            public ExtendedProductListBuilder(ISiteRules rules) : base(rules)
            {
            }
        }

        //the map profile that understands how to map from Product (entity) -> ExtendedProductModel. We don't have to add any
        //special mapping here since automapper will map the EanCode property from Product automatically.
        private class ExtendedProductItemMapProfile :
            ProductItemToCustomProductItemModelProfile<ExtendedProductItemModel>
        {
        }

        [TestMethod]
        public void CanBuildWithExtendedProductModel()
        {
            var rules = MockRepository.GenerateMock<ISiteRules>();

            //add the extended mapping needed to map the new property in the extended product model
            Mapper.Configuration.AddProfile<ExtendedProductItemMapProfile>();

            var productList = new ProductItemPagedList();
            var productItem = new ProductItem() {Id = 1, CampaignImage = "campagnImage.jpg"};
            productList.Items = new ProductItemList {productItem};
            productList.ItemCount = productList.Items.Count; //why is it like this?

            var builder = new ExtendedProductListBuilder(rules);

            var stormContext = MockRepository.GenerateMock<IStormContext>();
            stormContext.Stub(x => x.Configuration).Return(new StormConfigurationSection());
            stormContext.Stub(x => x.ShowPricesIncVat).Return(false);
            stormContext.Stub(x => x.CultureCode).Return("sv");
            StormContext.SetInstance(stormContext);

            var model = builder.BuildProductList(productList);

            Assert.AreEqual(productList.Items.Count, model.Items.Count);
            Assert.AreEqual(productList.Items[0].Id, model.Items[0].Id);
            Assert.AreEqual(productList.Items[0].CampaignImage, model.Items[0].CampaignImage);
        }
    }
}
